package handler

import (
	"github.com/gin-gonic/gin"
	"trpg-base/dto"
	"trpg-base/entity"
	"trpg-common/request"
	"trpg-common/response"
	"trpg-common/utils/gorm_util"
)

type ScriptsHandler struct{}

func (ScriptsHandler) AddOrUpdateScript(c *gin.Context) {
	scripts := entity.Scripts{}
	request.GetStructFromGinContext(c, &scripts)
	cover, err := c.FormFile("cover")
	if err != nil {
		cover = nil
	}
	content, err := c.FormFile("content")
	if err != nil {
		content = nil
	}
	user := request.GetUserFromGinContext(c)
	response.SuccessWithData(c, scriptsService.AddOrUpdateScript(scripts, cover, content, user))
}

func (ScriptsHandler) SearchScriptList(c *gin.Context) {
	scriptsRequest := dto.ScriptsRequest{}
	request.GetStructFromGinContext(c, &scriptsRequest)
	pageRequest := gorm_util.PageRequest{}
	pageRequest.Page = int32(request.GetIntFromQuery(c, "page"))
	pageRequest.PageSize = int32(request.GetIntFromQuery(c, "pageSize"))
	user := request.GetUserFromGinContext(c)
	response.SuccessWithData(c, scriptsService.SearchScripts(scriptsRequest, pageRequest, user))
}

func (ScriptsHandler) GetScriptById(c *gin.Context) {
	id := request.GetIntFromPath(c, "id")
	response.SuccessWithData(c, scriptsService.GetScriptById(id))
}

func (ScriptsHandler) CollectScripts(c *gin.Context) {
	scriptId := request.GetIntFromPath(c, "scriptId")
	user := request.GetUserFromGinContext(c)
	scriptsService.CollectScripts(scriptId, user)
	response.Success(c)
}

func (ScriptsHandler) DeleteScripts(c *gin.Context) {
	scriptId := request.GetIntFromPath(c, "scriptId")
	user := request.GetUserFromGinContext(c)
	scriptsService.DeleteCollection(scriptId, user)
	response.Success(c)
}
